#include "CProjectile.h"
#include "../core/CDirect3D.h"
#include "../core/CCamera.h"
#include "CObjectManager.h"

CProjectile::CProjectile(void)
{
}

CProjectile::~CProjectile(void)
{
}

void CProjectile::Initialize()
{
	m_fDepth = -1.0f;
	memset(&m_vfPosition, 0, sizeof(POINT));
	memset(&m_vfDirection, 0, sizeof(POINT));
	memset(&m_vfCollisionBox, 0, sizeof(RECT));
}

void CProjectile::Initialize(int posX, int posY, float depth, int dirX, int dirY, int collisionRadius)
{
	m_vfPosition.x = posX;
	m_vfPosition.y = posY;
	m_fDepth = depth;

	m_vfDirection.x = dirX;
	m_vfDirection.y = dirY;	

	m_vfCollisionBox.left	= -(collisionRadius);
	m_vfCollisionBox.top	= -(collisionRadius);
	m_vfCollisionBox.right	=  (collisionRadius);
	m_vfCollisionBox.bottom	=  (collisionRadius);
}

void CProjectile::Input(CRetroInput* pInput)
{
}

void CProjectile::Update(float fDeltaTime)
{
	memcpy(&m_vfPrevPosition, &m_vfPosition, sizeof(POINT));
	m_vfPosition.x += static_cast<LONG>(static_cast<float>(m_vfDirection.x) * 100.0f * fDeltaTime);
	m_vfPosition.y += static_cast<LONG>(static_cast<float>(m_vfDirection.y) * 100.0f * fDeltaTime);
}

void CProjectile::Render(CDirect3D* pRender, CCamera* pCamera)
{
	const POINT& camPos = pCamera->GetCameraOffset();
	RECT drawLine; // left-top = start // right-bottom = end
	drawLine.left	= m_vfPosition.x - m_vfDirection.x - camPos.x;
	drawLine.top	= m_vfPosition.y - m_vfDirection.y - camPos.y;
	drawLine.right	= m_vfPosition.x + m_vfDirection.x - camPos.x;
	drawLine.bottom = m_vfPosition.y + m_vfDirection.y - camPos.y;
	
	RECT drawRect;
	drawRect.left	= m_vfCollisionBox.left		+ m_vfPosition.x;
	drawRect.top	= m_vfCollisionBox.top		+ m_vfPosition.y;
	drawRect.right	= m_vfCollisionBox.right	+ m_vfPosition.x;
	drawRect.bottom = m_vfCollisionBox.bottom	+ m_vfPosition.y;
	
	// destroy is off screen
	bool onScreen = pCamera->RectOnScreen(drawRect);
	if(onScreen == false)
	{
		CObjectManager::GetInstance()->RemoveObject((IBaseObject*)this);
	}

	// put in local space to draw
	drawRect.left	-= camPos.x;
	drawRect.top	-= camPos.y;
	drawRect.right	-= camPos.x;
	drawRect.bottom -= camPos.y;

	pRender->DrawRect(drawRect, 128, 128, 128);
	pRender->DrawLine(drawLine.left, drawLine.top, drawLine.right, drawLine.bottom, 255, 255, 255);
}

void CProjectile::Destroy()
{
}

bool CProjectile::CheckCollision(IBaseObject* pObject)
{

	return false;
}

void CProjectile::SetPosition(LONG posX, LONG posY)
{
	m_vfPosition.x = posX;
	m_vfPosition.y = posY;
}

void CProjectile::SetPosition(POINT pos)
{
	SetPosition(pos.x, pos.y);
}